package com.ayf.payment.game.api.service.platform.impl;

import com.ayf.payment.game.api.dto.platform.SmsContentDTO;
import com.ayf.payment.game.api.entity.platform.SmsContent;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.mapper.SmsContentMapper;
import com.ayf.payment.game.api.service.platform.SmsContentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SmsContentServiceImpl implements SmsContentService{

    @Resource
    private SmsContentMapper smsContentMapper;

    @Override
    public List<SmsContent> getSmsContentList() {
        return smsContentMapper.selectAll();
    }

    @Override
    public void saveSmsContent(SmsContentDTO smsContentDTO) {
        if (smsContentDTO.getId() == null){ // 新增
            SmsContent smsContent = new SmsContent();
            smsContent.setSmsChannelCode(smsContentDTO.getSmsChannelCode());
            smsContent.setSmsType(smsContentDTO.getSmsType());
            int count = smsContentMapper.selectCount(smsContent);
            if (count>=1){
                throw new ParameterException("短信类型已存在");
            }
            smsContent.setSmsChannelName(smsContentDTO.getSmsChannelName());
            smsContent.setSmsContent(smsContentDTO.getSmsContent());
            smsContentMapper.insertSelective(smsContent);
        } else {
            SmsContent smsContent = new SmsContent();
            smsContent.setSmsChannelCode(smsContentDTO.getSmsChannelCode());
            smsContent.setSmsType(smsContentDTO.getSmsType());
            SmsContent originSmsContent = smsContentMapper.selectOne(smsContent);
            if (originSmsContent != null && !originSmsContent.getId().equals(smsContentDTO.getId())){
                throw new ParameterException("短信类型已存在");
            }
            smsContent.setSmsChannelName(smsContentDTO.getSmsChannelName());
            smsContent.setSmsContent(smsContentDTO.getSmsContent());
            smsContent.setId(smsContentDTO.getId());
            smsContentMapper.updateByPrimaryKeySelective(smsContent);
        }
    }

    @Override
    public void deleteSmsContent(SmsContentDTO smsContentDTO) {
        smsContentMapper.deleteByPrimaryKey(smsContentDTO.getId());
    }

    @Override
    public SmsContent getSmsContentBySmsChannelCodeAndSmsType(String smsChannelCode, String smsType) {
        SmsContent smsContent = new SmsContent();
        smsContent.setSmsChannelCode(smsChannelCode);
        smsContent.setSmsType(smsType);
        return smsContentMapper.selectOne(smsContent);
    }

}
